using System;
using UnityEditor;
using UnityEngine;

[CreateAssetMenu(menuName = "RPG Setup/Item Data/Material item", fileName = "Material data - ")]
[Serializable]
public class ItemDataSO : ScriptableObject
{
    public string saveId;

    [Header("----- Item Basic Detail -----")] 
    public string itemName;
    public Sprite itemIcon;
    public ItemType itemType;
    public int maxStackSize = 1;

    [Header("----- Drop Details -----")]
    [Range(0, 1000)] public float itemRarity = 200;
    [Range(0, 100)] public float dropChance;
    [Range(0, 100)] public float maxDropChance = 60;

    [Header("----- Effect Data -----")]
    public ItemEffect_DataSO effect;


    [Header("----- Craft Materials -----")]
    public ItemCraftMaterialInfo[] materials;

    [Header("----- Merchant detail-----")]
    public int price = 10;
    public int minStackSizeInShop = 1;
    public int maxStackSizeInShop = 1;

    private void OnValidate()
    {
        dropChance = GetDropChance();

#if UNITY_EDITOR
        //得到当前脚本的资源路径
        string path = AssetDatabase.GetAssetPath(this);
        //将获取到的资源路径转换成对应的GUID，GUID全局唯一标识符，不会因为该资源移动或者修改名字而改变
        saveId = AssetDatabase.AssetPathToGUID(path);
#endif
    }
    public float GetDropChance()
    {
        float maxItemRarity = 1000;
        float chance = (maxItemRarity - itemRarity + 1) / maxItemRarity * 100;

        return Mathf.Min(chance, maxDropChance);
    }
}


[Serializable]
public class ItemCraftMaterialInfo
{
    public ItemDataSO materialData;

    public int requireMaterialCount;
}
